#ifndef __VOICEDATASOURCE_H__
#define __VOICEDATASOURCE_H__

class PVMDataSourceU
{
protected:
	//common fields
	IPvmInStream*		stream;
	pvm_bool			opened;
	pvm_bool			used;
	pvm_bool			isOriginal;
	PVMCriticalSection	critical;
	pvm_bool			criticalInitialized;
protected:
	PVMDataSourceU(pvm_bool isOriginal)
	{
		stream = NULL;
		used = 0;
		this->isOriginal = isOriginal;
		criticalInitialized = PVMInitCritical(&critical);
	}
public:
	virtual ~PVMDataSourceU()
	{
		if(stream)
		{
			stream->Close();
			stream = NULL;
		}

		used = false;
		if(criticalInitialized)
			PVMDeinitCritical(&critical);
		criticalInitialized = false;
	}

	pvm_bool IsUsed()
	{
		pvm_bool retVal;

		PVMEnterCritical(&critical);
		retVal = used;
		PVMLeaveCritical(&critical);
		return retVal;
	}
	pvm_bool Use()
	{
		if(IsUsed())
			return false;
		PVMEnterCritical(&critical);
		used = true;
		PVMLeaveCritical(&critical);
		return true;
	}
	void UnUse()
	{
		if(!IsUsed())
			return;
		PVMEnterCritical(&critical);
		used = false;
		PVMLeaveCritical(&critical);
		if(!used && !isOriginal)
		{
			Dispose();
		}
	}

	IPvmInStream&	Stream() {return *stream;}

	virtual void Dispose()=0;
};

#endif